<template>
  <div class="permission-container">
    <el-tabs v-model="activeName">
      <el-tab-pane label="权限管理" name="first" />
    </el-tabs>
    <el-button
      type="primary"
      size="small"
      icon="icon-class"
      @click="addPermission('0', 1)"
    >添加权限</el-button>

    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" sortable label="菜单名称" />
      <el-table-column prop="code" sortable label="权限标识" />
      <el-table-column prop="description" sortable label="描述" />
      <el-table-column align="center" fixed="right" label="操作" width="240">
        <template #default="scope">
          <el-button
            v-if="scope.row.type === 1"
            size="mini"
            type="text"
            @click="addPermission(scope.row.id, 2)"
          >添加权限点</el-button>
          <el-button
            size="mini"
            type="text"
            @click="editPermission(scope.row.id)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            @click="delEmployees(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 放置一个弹层 用来编辑新增节点 -->
    <el-dialog
      :title="`${title}权限点`"
      :visible="showDialog"
      @close="btnCancel"
    >
      <!-- 表单 -->
      <el-form
        ref="perForm"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  delPermission,
  addPermission,
  getPermissionDetail,
  updatePermission
} from '@/api/permission'
import { fromData } from '@/utils/index'
export default {
  name: 'Permission',
  components: {},
  data() {
    return {
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型
        pid: '', // 父节点
        enVisible: '0' // 开启
      },
      activeName: 'first',
      tableData: [],
      loading: false,
      showDialog: false,
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    btnOK() {
      this.$refs.perForm.validate(async valid => {
        if (valid) {
          // 判断是新增还是编辑
          if (this.formData.id) {
            // 编辑
            await updatePermission(this.formData)
          } else {
            // 新增
            await addPermission(this.formData)
          }
          this.$message.success(
            this.formData.id ? '权限更新成功' : '权限添加成功'
          )
          this.getList()
          this.showDialog = false
        } else {
          this.$message.error('数据格式有误请检查')
          return false
        }
      })
    },
    // 点击取消
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.perForm.resetFields()
      this.showDialog = false
    },
    // 查询员工列表
    async getList() {
      this.loading = true
      const res = await getPermissionList()
      const rootTree = fromData(res, '0')

      this.tableData = rootTree
      setTimeout(() => {
        this.loading = false
      }, 300)
    },
    // 新增点击事件
    addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    async editPermission(id) {
      const res = await getPermissionDetail(id)
      // console.log('res:', res)
      this.formData = res
      this.showDialog = true
    },

    // 删除员工
    delEmployees(id) {
      this.$confirm('是否确认删除此权限吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delPermission(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss">
.permission-container {
  padding: 50px 15px 55px 60px;
  background-color: #fff;
  border-radius: 5px;
  .el-tree {
    position: absolute;
    z-index: 1;
    margin-top: 4px;
    width: 37%;
    border-radius: 3px;
    border: 1px solid #e4e7ed;
  }
  .el-table__header {
    th {
      background-color: #fafafa;
      border-bottom: 2px solid #e4e7ed;
    }
  }
  .info-box {
    display: inline-block;
    height: 100%;
    border: 1px solid #91d5ff;
    border-radius: 4px;
    line-height: 32px;
    padding: 2px 10px;
    background-color: #e6f7ff;
    i {
      margin-right: 5px;
      color: #409eff;
      font-size: 20px;
    }
    span {
      font-size: 14px;
    }
  }
  .el-checkbox-group {
    label {
      .el-checkbox__input.is-checked + .el-checkbox__label {
        color: #00a6a6;
      }
      .el-checkbox__input.is-checked .el-checkbox__inner {
        background-color: #00a6a6;
        border-color: #00a6a6;
      }
    }
  }
  .el-input__inner:focus {
    border-color: #00a6a6 !important;
  }
  .el-tabs__item.is-active,
  .el-tabs__item:hover,
  .el-button,
  .el-pager li.active {
    color: #00a6a6;
  }

  .el-button--primary,
  .el-dialog__title,
  .el-dialog__close {
    background-color: #00a6a6;
    color: #fff;
    border-color: transparent;
  }

  .el-tabs__active-bar,
  .el-dialog__header {
    background-color: #00a6a6;
  }
  .el-button--small,
  .el-pagination {
    margin: 10px 0 13px 15px;
    float: right;
  }
}
</style>
